{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基于用户的协同过滤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入必要的工具包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import pickle\n",
    "import scipy.io as sio\n",
    "import os\n",
    "\n",
    "import scipy.spatial.distance as ssd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#用户和item的索引\n",
    "user_index = pickle.load(open(\"user_index.pkl\",'rb'))\n",
    "item_index = pickle.load(open(\"item_index.pkl\",'rb'))\n",
    "\n",
    "n_users = len(user_index)\n",
    "n_items = len(item_index)\n",
    "\n",
    "#用户-物品关系矩阵\n",
    "user_item_scores = sio.mmread(\"user_items_scores\").todense()\n",
    "\n",
    "#倒排表\n",
    "user_items = pickle.load(open(\"user_items.pkl\",'rb'))\n",
    "item_users = pickle.load(open(\"item_users.pkl\",'rb'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#计算每个用户的平均打分\n",
    "mu = np.zeros(n_users)\n",
    "for u in range(n_users):\n",
    "    n_items_user = 0\n",
    "    r_acc = 0.0\n",
    "    \n",
    "    for i in user_items[u]:\n",
    "        r_acc += user_item_scores[u,i]\n",
    "        n_items_user += 1\n",
    "    \n",
    "    mu[u] = r_acc/n_items_user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.00757576, 0.16666667, 0.02222222, 0.14285714, 0.03448276,\n",
       "       0.0070922 , 0.01492537, 0.04545455, 0.01265823, 0.02325581,\n",
       "       0.01612903, 0.03125   , 0.05555556, 0.01851852, 0.01315789,\n",
       "       0.01136364, 0.01136364, 0.08333333, 0.02325581, 0.02      ,\n",
       "       0.06666667, 0.05882353, 0.03448276, 0.1       , 0.07142857,\n",
       "       0.00578035, 0.01886792, 0.04347826, 0.025     , 0.00719424,\n",
       "       0.02857143, 0.11111111, 0.01639344, 0.02325581, 0.02857143,\n",
       "       0.02777778, 0.16666667, 0.03448276, 0.02325581, 0.04166667,\n",
       "       0.01724138, 0.02222222, 0.01449275, 0.01923077, 0.5       ,\n",
       "       0.06666667, 0.00653595, 0.0045045 , 0.03571429, 0.00980392,\n",
       "       0.05      , 0.01351351, 0.03703704, 0.01176471, 0.01149425,\n",
       "       0.01098901, 0.05263158, 0.5       , 0.16666667, 0.01351351,\n",
       "       0.0625    , 0.02380952, 0.00662252, 0.00833333, 0.07142857,\n",
       "       0.00980392, 0.00502513, 0.02173913, 0.01111111, 1.        ,\n",
       "       0.01886792, 0.00684932, 0.01851852, 1.        , 0.02702703,\n",
       "       0.04166667, 0.01136364, 0.02777778, 0.01030928, 0.04545455,\n",
       "       0.06666667, 0.05555556, 0.00534759, 0.02272727, 0.05555556,\n",
       "       0.06666667, 0.02325581, 0.00598802, 0.00735294, 0.02941176,\n",
       "       0.02439024, 0.14285714, 0.2       , 0.0625    , 0.03125   ,\n",
       "       0.05882353, 0.02631579, 0.03846154, 0.25      , 0.00769231,\n",
       "       0.04545455, 0.11111111, 0.25      , 0.02777778, 0.02564103,\n",
       "       0.14285714, 0.01315789, 0.03333333, 0.03846154, 0.01282051,\n",
       "       0.02222222, 0.01785714, 0.00469484, 0.25      , 0.03225806,\n",
       "       0.05263158, 0.11111111, 0.16666667, 0.06666667, 0.07142857,\n",
       "       0.0106383 , 0.02631579, 0.25      , 0.05555556, 0.00980392,\n",
       "       0.14285714, 0.2       , 0.00564972, 0.14285714, 0.2       ,\n",
       "       0.03571429, 0.01298701, 0.03571429, 0.0212766 , 0.06666667,\n",
       "       0.08333333, 0.33333333, 0.06666667, 0.00680272, 0.02222222,\n",
       "       0.125     , 0.14285714, 0.00552486, 0.03125   , 0.14285714,\n",
       "       0.01190476, 0.1       , 0.01694915, 0.02857143, 0.01694915,\n",
       "       0.0625    , 0.125     , 0.05882353, 0.02439024, 0.01351351,\n",
       "       0.5       , 0.01176471, 0.02941176, 0.01639344, 0.06666667,\n",
       "       0.02777778, 0.04      , 0.33333333, 0.125     , 0.01408451,\n",
       "       1.        , 0.02173913, 0.03448276, 0.02702703, 0.01190476,\n",
       "       0.2       , 0.0049505 , 0.03125   , 0.04761905, 0.1       ,\n",
       "       0.01219512, 0.2       , 0.05263158, 0.00675676, 0.00934579,\n",
       "       0.01041667, 0.01298701, 0.01052632, 0.01851852, 0.03030303,\n",
       "       0.02083333, 0.16666667, 0.02777778, 0.01298701, 0.0075188 ,\n",
       "       0.05555556, 0.16666667, 0.03125   , 0.04761905, 0.03846154,\n",
       "       0.00454545, 0.00961538, 0.01282051, 0.0212766 , 0.16666667,\n",
       "       0.01612903, 0.00606061, 0.01851852, 0.2       , 0.11111111,\n",
       "       0.05555556, 0.01639344, 0.0125    , 0.00980392, 0.01851852,\n",
       "       0.01388889, 0.02702703, 0.02439024, 0.02564103, 0.00480769,\n",
       "       0.01265823, 0.02439024, 0.00900901, 0.07692308, 0.04545455,\n",
       "       0.0625    , 0.07142857, 0.02941176, 0.03846154, 0.0052356 ,\n",
       "       0.03846154, 0.04761905, 0.01298701, 0.03225806, 0.01020408,\n",
       "       0.07692308, 0.05882353, 0.04      , 0.07692308, 0.09090909,\n",
       "       0.08333333, 0.09090909, 0.04      , 0.01492537, 0.00505051,\n",
       "       0.01315789, 0.01851852, 0.05882353, 0.0625    , 0.08333333,\n",
       "       0.11111111, 0.01612903, 1.        , 0.01754386, 0.03448276,\n",
       "       0.0625    , 0.01428571, 1.        , 0.07692308, 0.01204819,\n",
       "       0.16666667, 0.02325581, 0.02777778, 0.03333333, 0.1       ,\n",
       "       0.03846154, 0.00621118, 0.33333333, 0.01538462, 0.16666667,\n",
       "       0.16666667, 0.01515152, 0.1       , 0.2       , 0.01754386,\n",
       "       0.02083333, 0.0125    , 0.0212766 , 0.1       , 1.        ,\n",
       "       0.03703704, 0.01086957, 0.05555556, 0.01190476, 0.03225806,\n",
       "       0.04347826, 0.01333333, 0.03225806, 0.03030303, 0.0625    ,\n",
       "       0.01282051, 0.04166667, 0.04      , 0.03448276, 0.01960784,\n",
       "       0.00943396, 0.00657895, 0.01204819, 1.        , 0.1       ,\n",
       "       0.01282051, 0.08333333, 0.00699301, 0.1       , 0.03846154,\n",
       "       0.03846154, 0.07692308, 0.01408451, 0.14285714, 0.04545455,\n",
       "       0.01333333, 1.        , 0.0625    , 0.04545455, 0.01694915,\n",
       "       0.00636943, 0.05882353, 0.02777778, 0.01265823, 0.5       ,\n",
       "       0.025     , 0.06666667, 0.02941176, 0.01724138, 0.01818182,\n",
       "       0.0125    , 0.03448276, 0.04347826, 0.2       , 0.01754386,\n",
       "       0.00404858, 0.01785714, 0.03125   , 0.0212766 , 0.00740741,\n",
       "       0.01666667, 0.02380952, 0.02325581, 0.01149425, 0.03225806,\n",
       "       0.03225806, 0.01754386, 0.01666667, 0.05555556, 0.0625    ,\n",
       "       1.        , 0.02702703, 0.00952381, 0.16666667, 0.03448276,\n",
       "       0.04545455, 0.02857143, 0.01515152, 0.5       , 0.02173913,\n",
       "       0.05263158, 0.25      , 0.05      , 0.025     , 0.008     ,\n",
       "       0.03448276, 0.01724138, 0.08333333, 0.00917431, 0.00367647,\n",
       "       0.02631579, 0.03703704, 0.03030303, 0.14285714, 0.02439024,\n",
       "       0.0075188 , 0.02439024, 0.004     , 0.0625    , 0.04761905,\n",
       "       0.02777778, 0.01538462, 0.01923077, 0.01639344, 0.11111111,\n",
       "       0.14285714, 0.02040816, 0.125     , 0.02083333, 0.16666667,\n",
       "       0.25      , 0.01538462, 0.03448276, 0.2       , 0.02380952,\n",
       "       0.03846154, 0.125     , 0.02439024, 0.01408451, 0.01030928,\n",
       "       0.01298701, 0.00724638, 0.03846154, 0.0106383 , 0.25      ,\n",
       "       0.07692308, 0.2       , 0.125     , 1.        , 0.01351351,\n",
       "       0.00478469, 0.02325581, 0.05263158, 0.02      , 0.09090909,\n",
       "       0.03225806, 0.07692308, 0.0070922 , 0.04      , 0.02040816,\n",
       "       0.01408451, 0.25      , 0.00840336, 0.02631579, 0.02222222,\n",
       "       0.00757576, 0.04545455, 0.01075269, 0.33333333, 0.01388889,\n",
       "       0.01851852, 0.01219512, 0.01515152, 0.03333333, 0.05882353,\n",
       "       0.07692308, 0.125     , 0.14285714, 0.01666667, 0.01851852,\n",
       "       0.0106383 , 0.00724638, 0.01612903, 0.2       , 0.03703704,\n",
       "       0.09090909, 0.02857143, 0.03333333, 0.01136364, 0.03703704,\n",
       "       0.33333333, 0.0625    , 0.01612903, 0.00763359, 0.0625    ,\n",
       "       0.06666667, 0.01111111, 0.03333333, 0.0625    , 0.00505051,\n",
       "       0.04347826, 0.125     , 0.02173913, 0.11111111, 0.0212766 ,\n",
       "       0.01960784, 0.125     , 0.01162791, 1.        , 0.07692308,\n",
       "       0.02631579, 0.00617284, 0.03448276, 0.00970874, 0.02      ,\n",
       "       0.07692308, 0.00502513, 0.08333333, 0.00833333, 0.01162791,\n",
       "       0.16666667, 0.00719424, 0.01369863, 0.1       , 0.01639344,\n",
       "       0.05882353, 0.00645161, 0.00704225, 0.01785714, 0.02380952,\n",
       "       0.00961538, 0.01123596, 0.06666667, 0.07692308, 0.0625    ,\n",
       "       0.00555556, 0.01694915, 0.02173913, 0.03846154, 0.01818182,\n",
       "       0.5       , 0.02564103, 0.00617284, 0.03448276, 0.03448276,\n",
       "       0.07692308, 0.11111111, 0.01724138, 0.02380952, 0.04761905,\n",
       "       0.03333333, 0.02040816, 1.        , 0.01041667, 0.01785714,\n",
       "       0.01282051, 0.03333333, 0.2       , 0.02857143, 0.05      ,\n",
       "       0.00990099, 0.01428571, 0.03448276, 0.05      , 0.03846154,\n",
       "       0.2       , 0.01351351, 0.03846154, 0.05555556, 0.03225806,\n",
       "       0.03225806, 0.04545455, 0.02439024, 0.16666667, 0.05555556,\n",
       "       0.02222222, 0.02272727, 0.25      , 0.1       , 0.05882353,\n",
       "       0.02222222, 0.03703704, 0.03846154, 0.03703704, 0.14285714,\n",
       "       0.03571429, 0.04545455, 0.01515152, 0.03703704, 0.16666667,\n",
       "       0.2       , 0.025     , 0.01449275, 0.14285714, 0.0212766 ,\n",
       "       0.0212766 , 0.03703704, 0.05263158, 0.09090909, 0.02380952,\n",
       "       0.125     , 0.04761905, 0.2       , 0.16666667, 0.04      ,\n",
       "       0.05882353, 0.04545455, 0.05      , 0.05555556, 0.1       ,\n",
       "       0.04347826, 0.01612903, 0.02439024, 1.        , 0.07692308,\n",
       "       0.11111111, 0.03846154, 0.03846154, 0.05263158, 1.        ,\n",
       "       0.01265823, 0.01428571, 0.08333333, 0.03333333, 0.25      ,\n",
       "       0.00581395, 0.01162791, 0.03125   , 0.01176471, 0.02      ,\n",
       "       0.05263158, 0.02631579, 0.16666667, 0.06666667, 0.07692308,\n",
       "       0.01818182, 0.01587302, 0.05      , 0.01666667, 0.02857143,\n",
       "       0.07692308, 0.04761905, 0.04      , 0.07142857, 0.00653595,\n",
       "       0.03225806, 0.00769231, 0.01724138, 0.04347826, 0.04      ,\n",
       "       0.125     , 0.00442478, 0.00917431, 0.02040816, 0.125     ,\n",
       "       0.09090909, 0.00869565, 0.03333333, 0.01851852, 0.04166667,\n",
       "       0.07692308, 0.0052356 , 0.00884956, 0.01351351, 0.00456621,\n",
       "       0.08333333, 0.02941176, 0.01724138, 0.1       , 0.03846154,\n",
       "       0.01219512, 0.05263158, 0.25      , 0.05882353, 0.02941176,\n",
       "       0.02222222, 0.01515152, 0.02272727, 0.125     , 0.01515152,\n",
       "       0.01388889, 0.0625    , 0.01111111, 0.01086957, 0.01612903,\n",
       "       0.00369004, 0.04166667, 0.1       , 0.5       , 0.05882353,\n",
       "       0.04166667, 0.04166667, 0.25      , 0.5       , 0.1       ,\n",
       "       0.02439024, 0.125     , 0.01315789, 0.00934579, 0.16666667,\n",
       "       0.11111111, 0.015625  , 0.01818182, 1.        , 0.04545455,\n",
       "       0.0212766 , 0.04545455, 0.14285714, 0.01666667, 0.04166667,\n",
       "       0.03125   , 0.02040816, 0.03703704, 0.00884956, 0.00925926,\n",
       "       0.2       , 0.0212766 , 0.02631579, 0.1       , 0.01449275,\n",
       "       0.00934579, 0.2       , 0.01123596, 0.07142857, 0.04      ,\n",
       "       0.02564103, 0.03846154, 0.5       , 0.11111111, 0.33333333,\n",
       "       0.01587302, 0.1       , 0.03571429, 0.03571429, 0.14285714,\n",
       "       0.01886792, 0.00446429, 0.2       , 0.06666667, 0.01587302,\n",
       "       1.        , 0.00769231, 0.00740741, 0.07692308, 0.01666667,\n",
       "       0.04      , 0.14285714, 0.03333333, 0.01724138, 0.125     ,\n",
       "       0.05555556, 0.00465116, 0.2       , 0.02380952, 0.01639344,\n",
       "       0.02564103, 0.02857143, 0.03846154, 0.16666667, 0.05263158,\n",
       "       0.0625    , 0.03846154, 0.01408451, 0.05555556, 0.025     ,\n",
       "       0.04761905, 0.01052632, 0.01492537, 0.2       , 0.01408451,\n",
       "       0.02941176, 0.03846154, 0.08333333, 0.14285714, 0.00826446,\n",
       "       0.03125   , 0.08333333, 0.01818182, 0.01724138, 0.04761905,\n",
       "       0.14285714, 0.33333333, 0.125     , 0.00609756, 0.07692308,\n",
       "       0.02857143, 0.02631579, 0.01428571, 0.0125    , 0.01      ,\n",
       "       0.02777778, 0.0212766 , 0.03225806, 0.02272727, 0.2       ,\n",
       "       0.16666667, 0.01111111, 0.04545455, 0.01960784, 0.02631579,\n",
       "       0.02631579, 0.00492611, 0.25      , 0.05263158, 0.02857143,\n",
       "       0.07142857, 0.00961538, 0.04761905, 0.01298701, 0.14285714,\n",
       "       0.1       , 0.01694915, 0.01388889, 0.02      , 0.015625  ,\n",
       "       0.02325581, 0.2       , 0.01587302, 0.5       , 0.01388889,\n",
       "       0.1       , 0.16666667, 0.05263158, 0.01538462, 0.00892857,\n",
       "       0.0078125 , 0.00671141, 0.11111111, 0.00763359, 0.0625    ,\n",
       "       0.04761905, 0.02564103, 0.05      , 0.11111111, 0.02439024,\n",
       "       0.01041667, 0.00943396, 0.09090909, 0.33333333, 0.01428571])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#计算两个用户之间的相似度\n",
    "uid1 = 0\n",
    "uid2 = 1\n",
    "si={}\n",
    "for item in user_items[uid1]:\n",
    "    if item in user_items[uid2]:\n",
    "        si[item]=1\n",
    "n = len(si)\n",
    "if (n!=0):\n",
    "    s1 = np.array([user_item_scores[uid1,item] for item in si])\n",
    "    s1 -= mu[uid1]\n",
    "    \n",
    "    s2 = np.array([user_item_scores[uid2,item] for item in si])\n",
    "    s2 -= mu[uid2]\n",
    "    \n",
    "    similarity = 1 - ssd.cosine(s1,s2)\n",
    "else:\n",
    "    similarity = 0.0;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def user_similarity(uid1,uid2):\n",
    "    si={}\n",
    "    for item in user_items[uid1]:\n",
    "        if item in user_items[uid2]:\n",
    "            si[item]=1\n",
    "    n = len(si)\n",
    "    if (n==0):\n",
    "        similarity = 0\n",
    "        return similarity\n",
    "    s1 = np.array([user_item_scores[uid1,item] for item in si])\n",
    "    s2 = np.array([user_item_scores[uid2,item] for item in si])\n",
    "    \n",
    "    similarity = 1 - ssd.cosine(s1,s2)\n",
    "    return similarity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#计算用户对每个item的预测打分\n",
    "cur_user = '6a944bfe30ae8d6b873139e8305ae131f1607d5f'\n",
    "\n",
    "cur_user_id = user_index[cur_user]\n",
    "cur_user_items = user_items[cur_user_id]\n",
    "\n",
    "sim_accumulate = 0.0\n",
    "rat_acc = 0.0\n",
    "\n",
    "user_items_scores = np.zeros(n_items)\n",
    "\n",
    "for i in range(n_items):\n",
    "    if i not in cur_user_items:\n",
    "        for user in item_users[i]:\n",
    "            sim = user_similarity(uid1 = user,uid2=cur_user_id)\n",
    "            \n",
    "            if sim !=0:\n",
    "                rat_acc += sim * (user_item_scores[user,i] - mu[user])\n",
    "                sim_accumulate += sim\n",
    "        if sim_accumulate != 0:\n",
    "            user_items_scores[i] = rat_acc/sim_accumulate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0.])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_items_scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "799 is not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-9-5a41f38bb9cc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msort_index\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m     \u001b[0mcur_item_index\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msort_index\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m     \u001b[0mcur_item\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlist\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mitem_index\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mitem_index\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mcur_item_index\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misnan\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msort_index\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mcur_item_index\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mcur_user_items\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mrank\u001b[0m \u001b[0;34m<=\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: 799 is not in list"
     ]
    }
   ],
   "source": [
    "#根据用户对item的预测打分产生top推荐\n",
    "sort_index = sorted(((e,i) for i,e in enumerate(list(user_items_scores))), reverse=True)\n",
    "\n",
    "columns = ['user_id','item','score','rank']\n",
    "df = pd.DataFrame(columns=columns)\n",
    "\n",
    "rank=1\n",
    "for i in range(0,len(sort_index)):\n",
    "    cur_item_index = sort_index[i][1]\n",
    "    cur_item = list (item_index.keys()) [list(item_index.values()).index (cur_item_index)]\n",
    "    \n",
    "    if -np.isnan(sort_index[i][0]) and cur_item_index not in cur_user_items and rank <=20:\n",
    "        df.loc[len(df)]=[cur_user,cur_item,sort_index[i][0],rank]\n",
    "        rank = rank+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item</th>\n",
       "      <th>score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [user_id, item, score, rank]\n",
       "Index: []"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
